package org.niu.listnode;

import java.math.BigDecimal;

public class AddList {
    public ListNode addInList (ListNode head1, ListNode head2) {
        // write code here
        StringBuilder s1 = new StringBuilder();
        StringBuilder s2 = new StringBuilder();
        while(head1!=null){
            s1.append(head1.val);
            head1=head1.next;
        }
        while(head2!=null){
            s2.append(head2.val);
            head2=head2.next;
        }
        BigDecimal ans1 = new BigDecimal(s1.toString());
        BigDecimal ans2 = new BigDecimal(s2.toString());
        BigDecimal ans = ans1.add(ans2);
        ListNode a = new ListNode(-1);
        ListNode tail = a;
        String s = ans.toString();
        for (int i = 0; i < s.length(); i++) {
            ListNode t = new ListNode(s.charAt(i)-'0');
            tail.next = t;
            tail = t;
        }
        return a.next;
    }
}
